﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace PRET.ReportControlLib.Controls
{
    public class SplitPanel : SplitContainer
    {
        #region ■宣言__________________________________________________________________

        private Button splitterButton;

        private Control oldParentControl = null;

        private event Action SplitterWidthChanged;

        private int minSplitterDistance = 0;

        private int previousSplitterDistance;

        private Point pntPosition = new Point(0, 0);

        #endregion

        #region ■属性____________________________________________________________

        /// <summary>  
        /// 分割线背景色
        /// </summary>  
        [Description("分割线背景色")]
        public Color _SplitButtonMouseDownBackColor
        {
            get
            {
                return this.splitterButton.FlatAppearance.MouseDownBackColor;
            }
            set
            {
                this.splitterButton.FlatAppearance.MouseDownBackColor = value;
                this.splitterButton.BackColor = value;
            }
        }

        /// <summary>  
        /// 鼠标移动到分隔线上时的颜色
        /// </summary>  
        [Description("鼠标移动到分隔线上时的颜色")]
        public Color _SplitButtonMouseOverBackColor
        {
            get
            {
                return this.splitterButton.FlatAppearance.MouseOverBackColor;
            }
            set
            {
                this.splitterButton.FlatAppearance.MouseOverBackColor = value;
            }
        }

        /// <summary>  
        /// 最小位置  
        /// </summary>  
        [Description("分割区域的最小宽度、高度")]
        [DefaultValue(10)]
        public int _MinSplitterDistance
        {
            get { return minSplitterDistance; }
            set { minSplitterDistance = value; }
        }

        /// <summary>  
        /// 分割线宽度
        /// </summary>  
        [Description("分割线宽度")]
        [DefaultValue(10)]
        public int _SplitterWidth
        {
            get
            {
                return this.SplitterWidth;
            }
            set
            {
                this.SplitterWidth = value;
                this.SplitterWidthChanged();
            }
        }

        [Browsable(false)]
        public new int SplitterWidth
        {
            get
            {
                return base.SplitterWidth;
            }
            set
            {
                base.SplitterWidth = value;
            }
        }

        #endregion

        #region ■分割按钮______________________________________________________________

        /// <summary>  
        /// 分割按钮初始化
        /// </summary>  
        private void InitButton()
        {
            splitterButton = new Button();
            splitterButton.Cursor = System.Windows.Forms.Cursors.Hand;
            if (this.Orientation == System.Windows.Forms.Orientation.Vertical)
                splitterButton.Size = new Size(this.SplitterWidth, 20);
            else
                splitterButton.Size = new Size(20, this.SplitterWidth);
            splitterButton.FlatAppearance.BorderSize = 0;
            splitterButton.FlatStyle = FlatStyle.Flat;
            splitterButton.BackColor = Color.Silver;
            splitterButton.Visible = true;
            splitterButton.Click += new EventHandler(btn_Click);
        }

        /// <summary>  
        /// 当分割区域折叠时重新计算分割线按钮的位置和大小
        /// </summary>  
        /// <param name="splitterWidth">分割线大小</param>  
        private void ResetButton()
        {
            if (this.Parent == null)
                return;

            if (this.Orientation == System.Windows.Forms.Orientation.Vertical)
            {
                pntPosition = new Point(this.SplitterRectangle.Left, (this.SplitterRectangle.Height - splitterButton.Height) / 2);
                splitterButton.Size = new Size(this.SplitterWidth, 20);
            }
            else
            {
                pntPosition = new Point((this.SplitterRectangle.Width - splitterButton.Width) / 2, this.SplitterRectangle.Top);
                splitterButton.Size = new Size(20, this.SplitterWidth);
            }
            pntPosition = this.PointToScreen(pntPosition);
            pntPosition = this.Parent.PointToClient(pntPosition);
            splitterButton.Left = pntPosition.X;
            splitterButton.Top = pntPosition.Y;
            splitterButton.BringToFront();
        }

        /// <summary>  
        /// OnCeateControl  
        /// </summary>  
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.ResetButton();
        }

        /// <summary>  
        /// 构造函数  
        /// </summary>  
        public SplitPanel() : this(0, 6)
        {
        }

        /// <summary>  
        /// 构造函数  
        /// </summary>  
        /// <param name="minSplitterDistance">分割区域1的最小高度/宽度</param>  
        /// <param name="defaultSplitterWidth">分割线的最小高度</param>  
        public SplitPanel(int minSplitterDistance, int defaultSplitterWidth)
            : base()
        {
            this.SplitterMoved += new SplitterEventHandler(SplitPanel_SplitterMoved);
            this.ParentChanged += new EventHandler(SplitPanel_ParentChanged);
            this.SplitterWidthChanged += new Action(this.ResetButton);
            this.SplitterWidth = defaultSplitterWidth;
            this.minSplitterDistance = minSplitterDistance;
            this.InitButton();

        }

        #endregion

        #region ■事件______________________________________________________________

        /// <summary>  
        /// 点击分割线时发生折叠或者展开动作  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void btn_Click(object sender, EventArgs e)
        {
            if (this.previousSplitterDistance == 0)
            {
                this.previousSplitterDistance = this.SplitterDistance;
            }
            if (this.previousSplitterDistance == this.SplitterDistance)
            {
                this.SplitterDistance = this.minSplitterDistance;
            }
            else
            {
                this.SplitterDistance = this.previousSplitterDistance;
            }
        }

        /// <summary>  
        ///   
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void SplitPanel_ParentChanged(object sender, EventArgs e)
        {
            if (oldParentControl != null)
                oldParentControl.Controls.Remove(splitterButton);

            if (this.Parent != null)
            {
                this.Parent.Controls.Add(splitterButton);
                this.ResetButton();
            }

            oldParentControl = this.Parent;
        }

        /// <summary>  
        /// 分割线移动时
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void SplitPanel_SplitterMoved(object sender, SplitterEventArgs e)
        {
            this.ResetButton();
        }

        #endregion
    } 
}
